// pages/material/material.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper: [],
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,      //是否自动切换
    interval: 3000,       //自动切换时间间隔
    duration: 1000,       //滑动动画时长
    inputShowed: false,
    inputVal: "",
    currentData: 0,    //当前页的data
    content: [],
    collect: [],
    videoImgs: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  navi: function (e) {
    let id = e.currentTarget.dataset.id;
    // console.log(id);
    wx.navigateTo({
      url: '/pages/mdetail/mdetail?id=' + id,
    })
    if (wx.getStorageSync('history') == '') {
      let list = [];
      list[0] = id;
      wx.setStorageSync('history', list);
    } else {
      let list = wx.getStorageSync('history');
      for (let i = 0; i < list.length; i++) {
        if (list[i] == id) {
          list.splice(i, 1);
        }
      }
      list.unshift(id);
      wx.setStorageSync('history', list);
    }
  },
  onWrite: function () {
    wx.navigateTo({
      url: '/pages/script/script'
    })
  },
  //获取当前滑块的index
  onCollect: function () {
    wx.request({
      url: `http://localhost:1234/material/collect?userid=${wx.getStorageSync('userid')}`,
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        // console.log(res.data);
        this.setData({
          collect: res.data
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  bindchange: function (e) {
    const that = this;
    // console.log(e.detail.current);
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;
    // console.log(e.target.dataset.current)
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.request({
      url: 'http://localhost:1234/material',
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        console.log(res.data);
        this.setData({
          content: res.data
        })
      },
      fail: err => {
        console.log(err);
      }
    });
    wx.request({
      url: 'http://localhost:1234/material/swiper',
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        console.log(res.data);
        this.setData({
          swiper: res.data
        })
      },
      fail: err => {
        console.log(err);
      }
    });
    if (!!wx.getStorageSync('userInfo')) {
      wx.switchTab({
        url: '/pages/material/material'
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    };
    this.onCollect();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})